package com.gzmu.face.serviceImpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gzmu.face.entity.Department;
import com.gzmu.face.entity.DmRelation;
import com.gzmu.face.mapper.DepartmentMapper;
import com.gzmu.face.service.DepartmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gzmu.face.service.DmRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobo Yan
 * @since 2020-04-19
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private DmRelationService dmRelationService;

    @Transactional
    @Override
    public int insertDepartment(Department department) {
        department.setRemark("部门");
        departmentMapper.insertDepartment( department );
        DmRelation dmRelation = new DmRelation();
        dmRelation.setDmid(department.getParent());
        dmRelation.setLowerid( department.getId() );
        dmRelation.setCreatetime( new Date(  ) );
        dmRelationService.insert( dmRelation );
        return 200;
    }


    @Transactional
    @Override
    public int deleteDepartment(String id) {
        departmentMapper.deleteById( id );
        dmRelationService.delete( new EntityWrapper<DmRelation>(  ).eq( "lowerid", id) );
        return 200;
    }
}
